// pages/my/orders.js
const app = getApp();
const action = require('../../utils/action.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabStatus:"all",
    models: {},
    page: 1,
    nextPage: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.status)
    {
      this.setData({tabStatus:options.status});
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.loadPage();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loadPage();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  switchTab: function (e) {
    console.log(e);
    if (this.data.tabStatus != e.currentTarget.dataset.status) {
      this.setData({ 
        tabStatus: e.currentTarget.dataset.status,
        page: 1,
        nextPage: true
        });
    }
    this.loadPage();
  },
  loadPage(){
    console.log("loadPage");
    var self = this;

    //已经有数据
    if (!self.data.nextPage) {
      console.log("已经是最后一页");
      return;
    }

    app.httpGet('/index.php?r=v1/order/index&status=' + self.data.tabStatus + '&page=' + self.data.page, {
      success: function (res) {
        console.log(res);
        if (res.data.page < res.data.total_page) {
          self.setData({ nextPage: true });
        }
        else {
          self.setData({ nextPage: false });
        }
        //第一页直接覆盖
        if (self.data.page == 1) {
          self.setData(
            { models: res.data }
          )
        }
        else {
          var oldModels = self.data.models;
          oldModels.page = res.data.page;
          oldModels.total = res.data.total;
          oldModels.total_page = res.data.total_page;
          oldModels.items = oldModels.items.concat(res.data.items);
          self.setData(
            { models: oldModels }
          )
        }
        console.log(self.data.models);
      }
    });

  },
  confirmOrder: function(e){
    var orderId = e.currentTarget.dataset.id;
    var url = "/pages/my/confirm?id="+orderId;
    wx.navigateTo({
      url: url,
    })
  },
  doPayment: function(e){
    var orderId = e.currentTarget.dataset.id;
    console.log(orderId);

    action.payOrder(this, orderId);
  }
})